import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:gczn_hd/app/core/base/base_view.dart';
import 'package:gczn_hd/app/core/base/handle_mixin.dart';
import 'package:gczn_hd/app/core/values/app_colors.dart';
import 'package:gczn_hd/app/core/values/App_img.dart';
import 'package:gczn_hd/app/core/values/app_values.dart';
import 'package:gczn_hd/app/routes/app_pages.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';


import '../../../core/values/text_styles.dart';
import '../controllers/login_controller.dart';

class LoginView extends BaseView<LoginController> with HandleMixin{

  LoginView(){
    controller.onViewInit();
  }


  final GlobalKey _globalKey = GlobalKey<FormState>();

  @override
  int get isFullBackground => 0;

  @override
  bool get isresizeBottomInset => false;

  final Size loginWrap = const Size(572, 708);
  final Size logoSize = const Size(207, 80);
  final double marginRight = 135;

  @override
  Widget body(BuildContext context){
    return  WillPopScope(
      onWillPop:  () async {
        return await _onWillPop(context);
    },
      child: Container(
        alignment: Alignment.topLeft,
        decoration: const BoxDecoration(
          color: AppColors.gcznPrimaryColor,
          image: DecorationImage(
            image: AssetImage(AppImg.loginBackground),
            fit: BoxFit.cover,
            alignment: Alignment.topLeft,
          ),
        ),
        child:  SizedBox(
          height: AppValues.pageHeight.h,
          child: Row(
            crossAxisAlignment: CrossAxisAlignment.center,
            mainAxisSize: MainAxisSize.max,
            children: [
              const Expanded(
                flex: 1, child: SizedBox(),
              ),
              Container(
                width: loginWrap.width.w,
                height: loginWrap.height.h,
                margin: EdgeInsets.only(right: marginRight.w),
                decoration: BoxDecoration(
                  color: AppColors.colorWhite.withOpacity(AppValues.decimals1),
                  border: Border.all(color: AppColors.loginWrapBorder),
                  borderRadius: BorderRadius.circular(AppValues.radius),
                ),
                child: Column(
                  mainAxisAlignment: MainAxisAlignment.start,
                  mainAxisSize: MainAxisSize.min,
                  children: [
                    SizedBox(height: AppValues.margin_40.h,),
                    Image.asset(AppImg.loginLogo, width: logoSize.width.w, height: logoSize.height.h,),
                    inputWrap(context),
                    SizedBox(height: AppValues.size32.h,),
                    Text("当前版本: V1.0.0", style: whiteTextStyle,)
                  ],
                ),
              )
            ],
          ),
        ),
      ),
    ) ;

  }

  // TODO  按两次退出应该, 未测试
  Future<bool> _onWillPop (BuildContext context) async {

    if ( DateTime.now().difference(controller.lastPressedAt.value) > const Duration(seconds: 2)) {
      controller.lastPressedAt.value = DateTime.now();
      Fluttertoast.showToast(msg: "再按一次退出应用");

      return false; // 不退出
    }
    SystemNavigator.pop();

    return true; // 退出应用
  }



  Widget inputWrap(BuildContext context){

    return  Container(
      margin: EdgeInsets.only(top: AppValues.size48.h , left: AppValues.size80.w, right: AppValues.size80.w),
      child:Form(
        key: _globalKey,
        child: Column(
          mainAxisSize: MainAxisSize.min,
          children: [
            buildTextField(textController: controller.accountController, hintText: "请输入账号", prefixIcon: AppImg.icon_account),
            SizedBox(height: AppValues.size45.h,),
            buildTextField(textController: controller.passwordController, hintText: "请输入密码", isPwd: true, prefixIcon: AppImg.icon_password),
            SizedBox(height: AppValues.size74.h,),
            buildButton(context)
          ],
        ),
      )
    );
  }
  Widget buildButton(BuildContext context){
    return SizedBox(
      width: MediaQuery.of(context).size.width,
      height: AppValues.size74.h,
      child: ElevatedButton(
        onPressed: (){

          if( controller.accountController.text == "" || controller.passwordController.text == "" ){

            showToast("请输入帐号密码");

            return ;
          }
          controller.login();

        },
        style: ButtonStyle(
          backgroundColor:WidgetStateColor.resolveWith((states) => AppColors.gcznPrimaryBlueColor,),
          shape: WidgetStateProperty.all<RoundedRectangleBorder>(
            RoundedRectangleBorder(
              borderRadius: BorderRadius.circular(6),
            ),
          ),
        ),
          child: Text("登录", style: TextStyle(
              color: AppColors.colorWhite,
              fontWeight: FontWeight.normal,
              fontSize: AppValues.size36.sp
          ),)
      ),
    );
  }



  Widget buildTextField({
    required TextEditingController textController,
    String hintText = "请输入",
    bool enabled = true,
    bool isPwd = false,
    required String prefixIcon,
  }){


    return TextFormField(
      controller: textController,
      enabled: enabled,
      obscureText: isPwd,
      cursorColor: AppColors.colorWhite,
      style: TextStyle(color: AppColors.colorWhite, fontSize: AppValues.size30.sp), // 设置输入文本颜色
      decoration: InputDecoration(
          filled: true,
          fillColor: AppColors.textFieldBackground,
          prefixIcon:  Padding(
            padding: const EdgeInsets.only(left: 10, right: 10),
            child: Image.asset(prefixIcon, width: AppValues.size26.w, height: AppValues.margin_30.h),
          ),
          prefixIconConstraints: const BoxConstraints(),
          // isCollapsed: false,
          hintText: hintText,
          hintStyle: TextStyle(
              fontWeight: FontWeight.w400,
              color: AppColors.textFieldHint,
              fontSize:AppValues.size30.sp
          ),
          border:  OutlineInputBorder(
              borderSide: const BorderSide(color: Color(0xFF4a5e76), width: 1),
              borderRadius: BorderRadius.all(Radius.circular(AppValues.margin_10.w))
          ),
          focusedBorder: const OutlineInputBorder(
            borderSide: BorderSide(color: AppColors.colorPrimary),
          ),
          contentPadding: const EdgeInsets.symmetric(horizontal: 20, vertical: 12),
      ),
    );
  }


  @override
  PreferredSizeWidget? appBar(BuildContext context)  => null;

}
